package com.buba.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;


import entity.device.DeviceType;
import entity.system.DepartmentManagement;
import com.buba.mapper.DepartmentManagementMapper;
import com.buba.service.DepartmentManagementService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 部门管理 服务实现类
 * </p>
 *
 * @author beisheng
 * @since 2023-05-18
 */
@Service
public class DepartmentManagementServiceImpl extends ServiceImpl<DepartmentManagementMapper, DepartmentManagement> implements DepartmentManagementService {
    @Autowired
    private DepartmentManagementMapper departmentManagementMapper;

    /**
     * @return java.util.List<entity.system.DepartmentManagement>
     * @Author atlong
     * @Description 树形查询部门信息
     * @Date 2023/6/5 9:25
     * @Param []
     **/
    @Override
    public List<DepartmentManagement> selAllDepartments() {
        QueryWrapper<DepartmentManagement> wrapper = new QueryWrapper<>();
        wrapper.eq("delete_flag", 0);
        wrapper.eq("department_state", 1);
        List<DepartmentManagement> departmentManagementList = departmentManagementMapper.selectList(wrapper);
        List<DepartmentManagement> collect = departmentManagementList.stream().filter(i ->
                Integer.valueOf(i.getParentDepartmentId()) == 0
        ).map(menu -> {
            menu.setChildren(getChidrens(menu, departmentManagementList));
            return menu;
        }).collect(Collectors.toList());
        return collect;
    }

    private List<DepartmentManagement> getChidrens(DepartmentManagement root, List<DepartmentManagement> all) {
        List<DepartmentManagement> children = all.stream().filter(carType -> {
            return Integer.valueOf(carType.getParentDepartmentId()).equals(root.getDepartmentManagementId());
        }).map(carType -> {
            carType.setChildren(getChidrens(carType, all));
            return carType;
        }).collect(Collectors.toList());
        return children;
    }

    /***
     * @Author atlong
     * @Description 查询启用部门信息
     * @Date 2023/6/14 9:44
     * @Param []
     * @return java.util.List<entity.system.DepartmentManagement>
     **/
    @Override
    public List<DepartmentManagement> selUserDepartment() {
        QueryWrapper<DepartmentManagement> wrapper = new QueryWrapper<>();
        wrapper.eq("delete_flag", 0);
        wrapper.eq("department_state", 1);
        List<DepartmentManagement> departmentManagementList = departmentManagementMapper.selectList(wrapper);
        return departmentManagementList;
    }

}
